/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import controllers.DatabaseController;
import controllers.MainWindowController;
import controllers.SearchController;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author Márcio Costa Junior - University of Sao Paulo
 */
public class SearchWindow extends javax.swing.JFrame {

    private DefaultListModel filesListModel;        //Modelo da lista de arquivos para seleção
    private DefaultListModel sizesListModel;        //Modelo da lista de tamanhos de arquivos para seleção
    private DefaultListModel seedsListModel;
    private DefaultListModel leechersListModel;
    private SearchController controller;
    /**
     * Creates new form SearchWindow
     */
    public SearchWindow(String file, MainWindowController main) {        
        this.setTitle("Distributed File Exchange");
        this.setLocation(350, 150);
        this.setSize(400, 400);
        
        filesListModel = new DefaultListModel();
        sizesListModel = new DefaultListModel();
        seedsListModel = new DefaultListModel();
        leechersListModel = new DefaultListModel();
        initComponents();
        this.sizesList.setModel(sizesListModel);
        this.seedsList.setModel(seedsListModel);
        this.leechersList.setModel(leechersListModel);
        
        controller = new SearchController(main, this);
        resultsPanel.setPreferredSize(new Dimension(resultsPanel.getWidth()-20, 20*this.filesListModel.getSize()));        
        filesList.setPreferredSize(new Dimension(filesList.getWidth(), 20*this.filesListModel.getSize()));
        sizesList.setPreferredSize(new Dimension(sizesList.getWidth(), 20*this.filesListModel.getSize()));
        seedsList.setPreferredSize(new Dimension(seedsList.getWidth(), 20*this.filesListModel.getSize()));
        leechersList.setPreferredSize(new Dimension(leechersList.getWidth(), 20*this.filesListModel.getSize()));
        //controller.listFiles();
        
    }
    
    public void addItem(String fileName, String fileSize, int seeds, int leechers){        
        filesListModel.addElement(fileName);
        sizesListModel.addElement(fileSize);
        seedsListModel.addElement(seeds);
        leechersListModel.addElement(leechers);
    }
    
    public void removeItem(int index){
        filesListModel.remove(index);
        sizesListModel.remove(index);
        seedsListModel.remove(index);
        leechersListModel.remove(index);
    }
    
    public void printDefaultMessage() {    
        filesListModel.addElement("No shared files in your network.");
        refresh();
    }
    
    public void refresh() {
        this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        downloadButton = new javax.swing.JButton();
        returnButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultsPanel = new javax.swing.JPanel();
        filesList = new javax.swing.JList();
        sizesList = new javax.swing.JList();
        seedsList = new javax.swing.JList();
        leechersList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        downloadButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        downloadButton.setText("Download");
        downloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downloadButtonActionPerformed(evt);
            }
        });

        returnButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        returnButton.setText("OK");
        returnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnButtonActionPerformed(evt);
            }
        });

        searchField.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N

        searchButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        searchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/magnifying_glass_icon.png"))); // NOI18N
        searchButton.setPreferredSize(new java.awt.Dimension(20, 20));
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        resultsPanel.setPreferredSize(new java.awt.Dimension(400, 192));

        filesList.setModel(filesListModel);

        javax.swing.GroupLayout resultsPanelLayout = new javax.swing.GroupLayout(resultsPanel);
        resultsPanel.setLayout(resultsPanelLayout);
        resultsPanelLayout.setHorizontalGroup(
            resultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(resultsPanelLayout.createSequentialGroup()
                .addComponent(filesList, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sizesList, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(seedsList, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(leechersList, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        resultsPanelLayout.setVerticalGroup(
            resultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(resultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(filesList, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addComponent(sizesList, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addComponent(seedsList, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addComponent(leechersList, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(resultsPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(returnButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(downloadButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchField, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(downloadButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(returnButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Download button
    private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
       controller.addDownload(filesList.getSelectedIndex());
    }//GEN-LAST:event_downloadButtonActionPerformed

    //Return button
    private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnButtonActionPerformed
        this.dispose();   
    }//GEN-LAST:event_returnButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        
        String text = searchField.getText();
        filesListModel.removeAllElements();
        sizesListModel.removeAllElements();
        seedsListModel.removeAllElements();
        leechersListModel.removeAllElements();
        refresh();
        controller.listFiles();
        
        if(!text.isEmpty()){
            controller.listFiles(text);
        }
    }//GEN-LAST:event_searchButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SearchWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SearchWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SearchWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SearchWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SearchWindow(null, null).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton downloadButton;
    private javax.swing.JList filesList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList leechersList;
    private javax.swing.JPanel resultsPanel;
    private javax.swing.JButton returnButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JList seedsList;
    private javax.swing.JList sizesList;
    // End of variables declaration//GEN-END:variables
}
